package com.iuie.ns.system.service.user.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.iuie.ns.frame.office.excel.OutputExcelUtil;
import com.iuie.ns.frame.persistence.dao.IGenericDao;
import com.iuie.ns.frame.service.impl.BaseServiceImpl;
import com.iuie.ns.frame.utils.MD5PasswordUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.system.dao.user.ISysUserDao;
import com.iuie.ns.system.mapper.user.SysUserMapper;
import com.iuie.ns.system.service.user.ISysUserService;
import com.iuie.ns.system.vo.role.SysRoleVO;
import com.iuie.ns.system.vo.user.SysUserVO;

/**
 * @description 系统用户管理实现类
 * @author lj
 * @createTime 2016年8月21日 下午3:11:54
 */
@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUserVO> implements ISysUserService {
	
	public static final String DEFAULT_USER_PWD = "iuie1234";
	
	public static final String ENCRYPTED_USER_PWD = MD5PasswordUtils.encode(DEFAULT_USER_PWD);
	
	@Resource(name="userDao")
	private ISysUserDao userDao;

	@Resource
	private SysUserMapper userMapper;
	
	@Override
	public void exportExcel(OutputStream os,SysUserVO user) throws RowsExceededException, WriteException, IOException {
		List<SysUserVO> lst = userDao.findAll(user, null);
		OutputExcelUtil.exportByPoi(os, SYS_USER_SHEET_TITLE, SYS_USER_HEADERS,lst,  "yyyy-MM-dd");
	}

	@Override
	public SysUserVO addOneUser(SysUserVO user) {
		Subject currentUser = SecurityUtils.getSubject();
		String userId = String.valueOf(currentUser.getPrincipal()); // 当前登录用户的用户id
//		String userId = "10000001";
		
		
		user.setUserPwd(DEFAULT_USER_PWD);
		user.setInsertUserId(userId); // 新增用户的用户信息
		user.setCreateTime(new Date()); // 插入数据时间
		user.setUserId(generateUserId());
		
		
		
		Serializable s = userDao.save(user);
		SysUserVO newUser = userDao.load(s);
		
		//ByteSource credentialsSalt = ByteSource.Util.bytes(user.getUserId()+SALT); // salt = AppConfigUtil.getInstance().getProperty("shiro.salt");
		//String encrypted =  new SimpleHash("MD5",userPwdOriginal,credentialsSalt,5).toHex();  // 加密后的密码
		newUser.setUserPwd(ENCRYPTED_USER_PWD);
		
		return newUser;
	}
	
	@Override
	public void saveUserRole(String userId, String[] roleIds) {
		userMapper.deleteUserAllRole(userId);
		if (roleIds != null && roleIds.length > 0 && userId !=null) {
			userMapper.saveUserRole(userId, roleIds);
		}
	}
	
	private String generateUserId() {
		String maxUserId = userMapper.queryMaxUserId();
		long userId = Long.parseLong(maxUserId) + 1l;
		return String.valueOf(userId);
	}
	

	@Override
	protected IGenericDao<SysUserVO, Serializable> getGenericDao() {
		return userDao;
	}

    @Override
    public List<String> findRoleCodesByUserId(String userId) {
        // 根据用户id查询用户拥有的角色的角色编码
        return null;
    }

    @Override
    public List<String> findPermissionCodesByUserId(String userId) {
        return null;
    }

	@Override
	public SysUserVO findByIdWithRoles(Serializable id) {
		// SysUserVO user = userDao.findByIdWithRoles(id);
		SysUserVO user = userMapper.findByIdWithRoles(id);
		return user;
	}
	@Override
	public SysUserVO findByIdWithWhole(String id) {
		SysUserVO user = userMapper.findByIdWithWhole(id);
		return user;
	}

	@Override
	public SysUserVO updateUserWithLogin(String userId) {
		// userMapper.updateUserWithLogin(userId,new Date());
		return null;
	}

	@Override
	public List<SysUserVO> findByPage(SysUserVO t, Page page) {
		PageHelper.startPage(page.getPage(), page.getRows());
		List<SysUserVO> list = userMapper.findByCondition(t);
		page.loadData(list);
		return list;
	}

	@Override
	public void updateOneUser(SysUserVO user) {
		userMapper.updateOneUser(user);
	}

	@Override
	public void resetUserPwd(String userId) {
		userMapper.resetUserPwd(userId, ENCRYPTED_USER_PWD);
	}

	@Override
	public void updateUserPwd(String userId, String userPwd) {
		String encryptedPwd = MD5PasswordUtils.encode(userPwd);
		userMapper.resetUserPwd(userId, encryptedPwd);
	}

	@Override
	public void updateCurrentRole(SysUserVO user, SysRoleVO role) {
		userMapper.updateCurrentRole(user, role);
	}

}
